package awk.script.javassist.method04.stub;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 监控类
 * @author yujianlong
 * @create 2021-03-28
 **/
public class StubMonitor {

	//id+描述信息的容器
	private static final ConcurrentHashMap<String, MethodDesc> metaContainer = new ConcurrentHashMap<>();

	/****
	 * 存放信息，返回id
	 *
	 * @author yujianlong
	 * @date 2021/3/29
	 * @return java.lang.String
	 */
	public static String getMetaDataId(MethodDesc methodDesc){
		String metaId = IdUtil.randomUUID();
		metaContainer.put(metaId, methodDesc);
		return metaId;
	}

	/***
	 * 成功输出信息
	 *
	 * @author yujianlong
	 * @date 2021/3/29
	 * @return void
	 */
	public static void printMonitorSuccess(String metaId,long startNanos,Object[] paraValues,Object returnValue){

		MethodDesc methodDesc = metaContainer.get(metaId);
		System.out.println("Success开始");
		System.out.println("类.方法：" + methodDesc.getClazzName()+"."+(methodDesc.isStaticFlag()?"静态":"非静态") + "." + methodDesc.getMethodName());
		System.out.println("入参名称：" +methodDesc.getParameterNames() + " 入参[类型]：" + methodDesc.getParameterTypes() + " 入数[值]：" + JSON.toJSONString(paraValues));
		System.out.println("出参：" + methodDesc.getReturnType() + " 出参[值]：" + JSON.toJSONString(returnValue));
		System.out.println("耗时：" + (System.nanoTime() - startNanos));
		System.out.println("Success结束");


	}

	/***
	 * 失败输出信息
	 *
	 * @author yujianlong
	 * @date 2021/3/29
	 * @return void
	 */
	public static void printMonitorFail(String metaId,Throwable throwable){
		MethodDesc methodDesc = metaContainer.get(metaId);
		System.out.println("fail开始");
		System.out.println("类.方法：" + methodDesc.getClazzName()+"."+(methodDesc.isStaticFlag()?"静态":"非静态") + "." + methodDesc.getMethodName());
		System.out.println("异常：" + throwable.getMessage());
		System.out.println("fail结束");

	}


}
